import { computed, ComputedRef, Ref, ref } from "vue";
import { defineStore } from "pinia";
import { fetchSpuListAPI, deleteSpuAPI, fetchTrademarkListAPI, fetchSpuImageListAPI, fetchSpuSaleAttributeListAPI, fetchBaseSaleAttributeListAPI } from "@/api/product/spuApi/spu.ts";
import { pageParamsInterface } from "@/views/product/trademark/interface/interface.ts";
import { baseSaleAttribute, saleAttributeInfo, spu, spuImage, spuInfo } from "@/api/product/spuApi/interface.ts";
import { spuStore } from "@/pinia/modules/product/spuStore/interface.ts";
import { trademark } from "@/api/product/trademarkApi/interface.ts";

const useSpuStore = defineStore("spuStore", (): spuStore => {
  /* spu的列表信息 */
  const spuListInfo: Ref<spuInfo | undefined> = ref<spuInfo | undefined>();
  /* 总共的数据条数 */
  const total: ComputedRef<number | undefined> = computed<number | undefined>(() => spuListInfo.value?.total);
  /* 场景值 0:spu列表 1:添加或修改SPU 2:添加SKU */
  const screen: Ref<number> = ref<number>(0);
  /* 是否是编辑状态 */
  const edit: Ref<boolean> = ref<boolean>(false);
  /*spu列表*/
  const spuList: ComputedRef<spu[] | undefined> = computed<spu[] | undefined>(() => spuListInfo.value?.records);
  /* spu所有的品牌列表 */
  const trademarkList: Ref<trademark[]> = ref<trademark[]>([]);
  /* spu的已有图片列表 */
  const spuImageList: Ref<spuImage[]> = ref<spuImage[]>([]);
  /*spu已有的销售属性列表*/
  const spuSaleAttributeList: Ref<saleAttributeInfo[]> = ref<saleAttributeInfo[]>([]);
  /*spu所有的销售属性列表*/
  const baseSaleAttributeList: Ref<baseSaleAttribute[]> = ref<baseSaleAttribute[]>([]);
  /* 从所有的销售属性列表中过滤出当前spu销售列表中没有的销售属性列表 */
  const selectSaleAttribute: ComputedRef<baseSaleAttribute[]> = computed<baseSaleAttribute[]>(() => {
    return baseSaleAttributeList.value.filter(baseSaleAttr => {
      return spuSaleAttributeList.value.every(saleAttr => baseSaleAttr.name !== saleAttr.saleAttrName);
    });
  });
  /**/
  const saleAttrIdAndName: Ref<string> = ref<string>("");
  /* 当前的spu */
  const currentSpu: Ref<spu | null> = ref<spu | null>(null);
  /* 切换场景的回调函数 */
  const changeScreen: (flag: number) => void = (flag: number): void => {
    screen.value = flag;
  };
  /* 切换编辑状态的回调函数 */
  const changeEdit: (flag: boolean) => void = (flag: boolean): void => {
    edit.value = flag;
  };
  /* 发送请求获取spu列表信息的回调函数 */
  const fetchSpuList: (pageParams: pageParamsInterface, category3Id: number | string) => Promise<void> = async (pageParams: pageParamsInterface, category3Id: number | string): Promise<void> => {
    const { data }: { data: spuInfo } = await fetchSpuListAPI(pageParams, category3Id);
    spuListInfo.value = data;
  };
  /* 发送请求获取spu所有品牌列表的回调函数 */
  const fetchTrademarkList: () => Promise<void> = async (): Promise<void> => {
    const { data }: { data: trademark[] } = await fetchTrademarkListAPI();
    trademarkList.value = data;
  };
  /* 发送请求获取spu已有图片列表的回调函数 */
  const fetchSpuImageList: (spuId: number) => Promise<void> = async (spuId: number): Promise<void> => {
    const { data }: { data: spuImage[] } = await fetchSpuImageListAPI(spuId);
    data.forEach(item => {
      item.url = item.imgUrl;
      item.name = item.imgName;
    });
    spuImageList.value = data;
  };
  /* 发送请求获取spu已有销售属性列表的回调函数 */
  const fetchSpuSaleAttributeList: (spuId: number) => Promise<void> = async (spuId: number): Promise<void> => {
    const { data }: { data: saleAttributeInfo[] } = await fetchSpuSaleAttributeListAPI(spuId);
    data.forEach(item => item.flag = false);
    spuSaleAttributeList.value = data;
  };
  /* 发送请求获取spu所有销售属性列表的回调函数 */
  const fetchBaseSaleAttributeList: () => Promise<void> = async (): Promise<void> => {
    const { data }: { data: baseSaleAttribute[] } = await fetchBaseSaleAttributeListAPI();
    baseSaleAttributeList.value = data;
  };
  /* 发送请求删除spu的回调函数 */
  const deleteSpu: (spuId: number) => Promise<string> = async (spuId: number): Promise<string> => {
    await deleteSpuAPI(spuId);
    return "删除成功";
  };
  /* 查找当前spu的的回调函数 */
  const findSpu = (spuInfo: spu) => {
    currentSpu.value = spuInfo;
  };

  return {
    spuListInfo,
    total,
    spuList,
    screen,
    edit,
    trademarkList,
    spuImageList,
    spuSaleAttributeList,
    baseSaleAttributeList,
    currentSpu,
    selectSaleAttribute,
    saleAttrIdAndName,
    fetchSpuList,
    changeScreen,
    changeEdit,
    deleteSpu,
    fetchTrademarkList,
    fetchSpuImageList,
    fetchSpuSaleAttributeList,
    fetchBaseSaleAttributeList,
    findSpu,
  };
});

export default useSpuStore;
